package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import beans.BranchSMSTemplate;

import utils.HibernateUtil;




public class BranchSMSTemplateDao {
	private SessionFactory sessionFactory;

	public BranchSMSTemplateDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;

	/**
	 * 返回所有短信模板
	 */
	public List<BranchSMSTemplate> getAllTemp() {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(BranchSMSTemplate.class);
		List<BranchSMSTemplate> templates = c.list();
		tr.commit();
		session.close();
		return templates;
	}

	/**
	 * 根据设备网点id返回短信模板集合
	 */
	public List<BranchSMSTemplate> getByBranchId(int branchId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(BranchSMSTemplate.class);
		c.add(Restrictions.eq("branchId", branchId+""));
		List<BranchSMSTemplate> templates = c.list();
		tr.commit();
		session.close();
		return templates;
	}

	/**
	 * 删除短信模板
	 */
	public void delete(BranchSMSTemplate template) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(template);
		tr.commit();
		session.close();
	}

	/**
	 * 修改短信模板
	 */
	public void edit(BranchSMSTemplate template) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(template);
		tr.commit();
		session.close();
	}

	/**
	 * 保存新增的短信模板
	 */
	public BranchSMSTemplate save(BranchSMSTemplate template) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(template);
		tr.commit();
		session.close();
		return template;
	}
}
